import axios from 'axios'
import mockCreator from '@/mock'

const dataPromise = import('@/mock/mockData')
const mock = mockCreator(dataPromise, true)

export default class Service {
    // @mock('getData')
    static login (userName, password) {
        return axios.post(`/api/doLogin`, { userName, password }).then(res => res.data)
    }

    static register (userName, password, email) {
        return axios.post(`/api/regUser`, { userName, password, email }).then(res => res.data)
    }

    // @mock('getHomeData')
    static getHomeData () {
        return axios.get(`/api/findGoods`).then(res => {
            let data = res.data.obj
            return data
        })
    }

    static getCartData (userId) {
        return axios.post(`/api/findCart`, { userId }).then(res => {
            return res.data.obj
        })
    }

    static addToCart (goodId, userId) {
        return axios.post(`/api/insertGoodIntoCart`, { goodId, userId }).then(res => res.data)
    }

    static delCartData (userId, goodId, id) {
        return axios.post(`/api/delCart`, { userId, goodId, id }).then(res => res.data)
    }

    static getOrderData (userId) {
        return axios.post(`/api/findOrder`, { userId }).then(res => res.data.obj)
    }

    // @mock('getAllOrderData')
    static getAllOrderData () {
        return axios.get(`/api/findAllOrder`).then(res => res.data.obj)
    }

    static insertOrder (userId, goodId, id) {
        return axios.post(`/api/insertOrder`, {userId, goodId, id}).then(res => res.data.obj)
    }

    // @mock('getUserData')
    static getUserData () {
        return axios.get(`/api/findAllUser`).then(res => res.data.obj)
    }

    // @mock('delUser')
    static delUser (id) {
        return axios.post (`/api/delUser`, {id}).then(res => res.data) 
    }

    // @mock('uploadImg')
    static uploadImg (img) {
        console.log("img>>>", img)
        return axios.post(`/api/upLoadImg`, img, {
            headers: {
                "withCredentials": true,
                "Content-Type": "multipart/form-data"
            }
        }).then(res => res.data.obj)
    }

    // @mock('inserGood')
    static insertGood ( goodName, content, price, img ) {
        return axios.post(`/api/insertGood`, { goodName, content, price, img}).then(res => res.data)
    }

    static delGood (id) {
        return axios.post(`/api/delGood`, {id}).then(res => res.data)
    }

}
